package com.dream52.pms.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Repository
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {

	public default T getById(int key) {
		return this.selectByPrimaryKey(key);
	}

	public default T getByWhere(Class<T> clazz, String whereStr) {

		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();
		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		List<T> list = this.selectByExample(example);

		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public default List<T> getListByWhere(Class<T> clazz, String whereStr, String orderStr) {

		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();

		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		if (orderStr != null && orderStr.length() > 0) {
			example.setOrderByClause(orderStr);
		}

		List<T> list = this.selectByExample(example);

		return list;
	}

	public default List<T> getPageListByWhere(Class<T> clazz, String whereStr, int pageno, int pagesize,
			String orderStr) {

		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();
		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		if (orderStr != null && orderStr.length() > 0) {
			example.setOrderByClause(orderStr);
		}

		RowBounds r = new RowBounds(pageno, pagesize);

		List<T> list = this.selectByExampleAndRowBounds(example, r);

		return list;
	}

	public default PageInfo<T> getPageByWhere(Class<T> clazz, String whereStr, int pageno, int pagesize,
			String orderStr) {
		PageHelper.startPage(pageno, pagesize);
		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();

		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		if (orderStr != null && orderStr.length() > 0) {
			example.setOrderByClause(orderStr);
		}

		List<T> list = this.selectByExample(example);
		PageInfo<T> pageInfo = new PageInfo<T>(list);
		PageHelper.clearPage();

		return pageInfo;
	}

	public default int getCountByWhere(Class<T> clazz, String whereStr) {
		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();

		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		return this.selectCountByExample(example);
	}

	public default int save(T entity) {
		return this.insert(entity);
	}

	public default int update(T entity) {
		return this.updateByPrimaryKeySelective(entity);
	}

	public default int updateByWhere(Class<T> clazz, T entity, String whereStr) {
		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();

		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		return this.updateByExampleSelective(entity, criteria);
	}

	public default int deleteById(int id) {
		return this.deleteByPrimaryKey(id);
	}

	public default int deleteByWhere(Class<T> clazz, String whereStr) {
		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();

		if (whereStr != null && whereStr.length() > 0) {
			criteria.andCondition(whereStr);
		}

		return this.deleteByExample(example);
	}
}
